package com.example.hxd.zndzg.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.hxd.zndzg.R;
import com.example.hxd.zndzg.adapter.MyFragmentPagerAdapter;
import com.example.hxd.zndzg.fragment.AboutFragmeng;
import com.example.hxd.zndzg.fragment.BoxsFragmeng;
import com.example.hxd.zndzg.fragment.SettingFragmeng;
import com.example.hxd.zndzg.view.NoScrollViewPager;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {


    private NavigationView navigationView;
    private DrawerLayout mDrawerLayout;
    private DrawerLayout drawerLayout;
    private SystemBarTintManager tintManager;
    private Context mContext;
    private SharedPreferences.Editor spPasswordEdit;
    private SharedPreferences spPassword;
    private String password;
    private ImageView mainMenu;



    private List<Fragment> fragmentList;
    private NoScrollViewPager myViewPager;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    private int mCurrentDialogStyle = com.qmuiteam.qmui.R.style.QMUI_Dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initWindow();
        initView();
    }


    private void initView() {
        navigationView = findViewById(R.id.nav);
        mDrawerLayout = findViewById(R.id.activity_na);
        drawerLayout = (DrawerLayout) findViewById(R.id.activity_na);
        mainMenu = findViewById(R.id.main_menu);
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); //关闭手势滑动
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); //打开手势滑动
        mContext = HomeActivity.this;
        spPassword = getSharedPreferences("AdministratorsPassword", Context.MODE_PRIVATE);
        spPasswordEdit = getSharedPreferences("AdministratorsPassword", Context.MODE_PRIVATE).edit();
        password = spPassword.getString("password", null);
        myViewPager = findViewById(R.id.vp_view);
        //把三个Fragment添加到List中
        fragmentList = new ArrayList<>();
        fragmentList.add(new BoxsFragmeng(mContext, HomeActivity.this));
        fragmentList.add(new SettingFragmeng(mContext, HomeActivity.this));
        fragmentList.add(new AboutFragmeng(mContext));
        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        myViewPager.setAdapter(myFragmentPagerAdapter);
        myViewPager.setCurrentItem(0);
        myViewPager.setNoScroll(true);

        mainMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawerLayout.isDrawerOpen(navigationView)){
                    drawerLayout.closeDrawer(navigationView);
                }else{
                    drawerLayout.openDrawer(navigationView);
                }
            }
        });

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        myViewPager.setCurrentItem(0);
                        mDrawerLayout.closeDrawers();
                        break;

                    case R.id.setting_psw:
                        showPswDialog();
                        //Toast.makeText(mContext, "你点击了设置管理员密码", Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.setting:
                        VerificationPswDialog();
                        break;

                    case R.id.about:
                       myViewPager.setCurrentItem(2);
                        mDrawerLayout.closeDrawers();
                        break;
                }
                return true;
            }
        });
    }

    //初始化窗口属性，让状态栏和导航栏透明
    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            tintManager = new SystemBarTintManager(this);
            int statusColor = Color.parseColor("#1976d2");
            tintManager.setStatusBarTintColor(statusColor);
            tintManager.setStatusBarTintEnabled(true);
        }
    }

    /**
     * 用于初始换和修改管理员密码
     */
    private void showPswDialog() {
        if (password == null){
            final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(this);
            builder.setTitle("初始化管理员密码")
                    .setPlaceholder("在此输入您的管理员密码")
                    .setInputType(InputType.TYPE_CLASS_TEXT)
                    .addAction("取消", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            dialog.dismiss();
                        }
                    })
                    .addAction("确定", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            String psw = builder.getEditText().getText().toString();
                            spPasswordEdit.putString("password", psw);
                            spPasswordEdit.commit();
                            dialog.dismiss();
                            Toast.makeText(mContext, "管理员密码设置成功", Toast.LENGTH_SHORT).show();
                            password = spPassword.getString("password", null);
                        }
                    })
                    .create(mCurrentDialogStyle).show();
        }else {
            final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(this);
            builder.setTitle("修改管理员密码")
                    .setPlaceholder("在此输入您之前的管理员密码")
                    .setInputType(InputType.TYPE_CLASS_TEXT)
                    .addAction("取消", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            dialog.dismiss();
                        }
                    })
                    .addAction("确定", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            String psw = builder.getEditText().getText().toString();
                           if (psw.equals(password)){
                               dialog.dismiss();
                               password = null;
                               showPswDialog();
                           }else {
                               builder.getEditText().setText("");
                               Toast.makeText(mContext, "管理员密码有误，请重新输入", Toast.LENGTH_SHORT).show();
                           }
                        }
                    })
                    .create(mCurrentDialogStyle).show();
        }

    }

    /**
     * 验证管理员密码
     */
    private void VerificationPswDialog() {
        if (password == null){
            Toast.makeText(mContext, "您还未初始化管理员密码", Toast.LENGTH_SHORT).show();
        }else {
            final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(mContext);
            builder.setTitle("验证管理员密码")
                    .setPlaceholder("在此输入您的管理员密码")
                    .setInputType(InputType.TYPE_CLASS_TEXT)
                    .addAction("取消", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            dialog.dismiss();
                        }
                    })
                    .addAction("确定", new QMUIDialogAction.ActionListener() {
                        @Override
                        public void onClick(QMUIDialog dialog, int index) {
                            String psw = builder.getEditText().getText().toString();
                                if (psw.equals(password)){
                                    myViewPager.setCurrentItem(1);
                                    dialog.dismiss();
                                    mDrawerLayout.closeDrawers();
                                }else {
                                    builder.getEditText().setText("");
                                    Toast.makeText(mContext, "管理员密码输入有误，请重新输入", Toast.LENGTH_SHORT).show();
                                }
                        }
                    })
                    .create(mCurrentDialogStyle).show();
        }

    }
}
